package iface.aplicativo.apresentacao.converter;

import iface.arquitetura.comum.string.UtilString;

import java.text.DecimalFormat;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "moedaDoubleConverter")
public class MoedaDoubleConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
		Double vlrMoeda = null;

		if (!UtilString.isVazio(string)) {
			string = string.replace("R$ ", "").replace(".", "")
					.replace(",", ".");
			vlrMoeda = Double.valueOf(string);
		}

		return vlrMoeda;
	}

	public String getAsString(FacesContext fc, UIComponent uic, Object o) {
		Double val = (Double) o;
		if (val == null) {
			return null;
		}
		DecimalFormat format = new DecimalFormat("R$ ,##0.00");
		return format.format(val);
	}

}